package com.example.demo.thread;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * @author chenchao
 * @desc
 * @date 2022/2/17 16:47
 */
@Configuration
public class Executors {
    @Resource
    private Executors executors;

    public void execute() {
        executors.newThreadPool().execute(null);
    }

    @Bean
    public ThreadPoolExecutor newThreadPool() {
        return new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors() + 1,
                Runtime.getRuntime().availableProcessors() * 2,
                60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(500),
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r, "111");
                        thread.setDaemon(true);
                        return thread;
                    }
                },
                new ThreadPoolExecutor.CallerRunsPolicy()


        );
    }
}
